package com.maochang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maochang.mapper.*;
import com.maochang.pojo.*;
import com.maochang.pojo.util.ChuAnSuoKuAng;
import com.maochang.pojo.util.ZhuRenWuXiaoQueMingXi;
import com.maochang.service.xiaoQuZhiDingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service("xiaoQuZhiDingImpl")
public class xiaoQuZhiDingImpl extends ServiceImpl<PsSolvetaskMainMapper, PsSolvetaskMain> implements xiaoQuZhiDingService {
    @Resource
    private PsSolvetaskMainMapper psSolvetaskMainMapper;//主表操 做
    @Resource
    private PsSysTemConfigMapper psSysTemConfigMapper;
    @Resource
    private PsInspectiontaskMainMapper psInspectiontaskMainMapper;
    @Resource
    private PsInspectiontaskDetailMapper psInspectiontaskDetailMapper;
    @Resource
    private PsLineInfoMapper psLineInfoMapper;
    @Resource
    private PsPoleInfoMapper psPoleInfoMapper;
    @Resource
    private PsUserMapper psUserMapper;
    @Resource
    private  PsSolvetaskDetailMapper  psSolvetaskDetailMapper; //消缺子表 数据操作
    @Resource
    private   PsSolverDetailMapper psSolverDetailMapper;

    @Override
    public Page<PsSolvetaskMain> CHaXunZiRenWu(PsSolvetaskMain ps, int page, int size, String kai, String jie, HttpSession session) {
        System.out.println(ps.getTaskStatus()+"状态");
        int  aa=0;
            Page<PsSolvetaskMain> psliPage=new Page<>(page,size);
            QueryWrapper<PsSolvetaskMain> wrapper= Wrappers.query();
            wrapper.like(!StringUtils.isBlank(ps.getSolveTaskCode()),"solveTaskCode",ps.getSolveTaskCode())
                .eq(ps.getWorkDocType()!=null,"solveTaskName",ps.getSolveTaskName())
                .ge(!StringUtils.isBlank(ps.getIssuedByName()),"issuedByName",ps.getIssuedByName())
                .ge(!StringUtils.isBlank(kai),"issuedTime",kai)
                .le(!StringUtils.isBlank(jie),"issuedTime",jie);
            if(ps.getTaskStatus()!=null&&ps.getTaskStatus()!=aa){
               wrapper.eq("taskStatus",ps.getTaskStatus());
            }
            return psSolvetaskMainMapper.AllPXjList(psliPage,wrapper);
    }

    /**
     * 增加缺陷任务信息
     * @param xi
     * @param httpSession
     * @return
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Boolean addXiaoQu(ZhuRenWuXiaoQueMingXi xi, HttpSession httpSession) {
      PsUser  yongHu = (PsUser) httpSession.getAttribute("loginUser");
    //第一步先 插入主任务
        //任务编号    --前端赋值
        PsSolvetaskMain psSolvetaskMain=new PsSolvetaskMain();
        //消缺任务名称  --前端赋值
        psSolvetaskMain.setSolveTaskCode(xi.getPsSolvetaskMain().getSolveTaskCode());
        psSolvetaskMain.setSolveTaskName(xi.getPsSolvetaskMain().getSolveTaskName());
        //工作单据id   --前端赋值
        psSolvetaskMain.setWorkDocType(Integer.valueOf(xi.getPsSolvetaskMain().getWorkDocTypeName()));
        QueryWrapper<PsSystemconfig> sywr= Wrappers.query();
        sywr.eq("id",xi.getPsSolvetaskMain().getWorkDocTypeName());
        PsSystemconfig systemconfig =psSysTemConfigMapper.selectOne(sywr);
        //工作单据name --这个需要查
        psSolvetaskMain.setWorkDocTypeName(systemconfig.getConfigName());
        //任务描述     --前端赋值
        psSolvetaskMain.setTaskDesc(xi.getPsSolvetaskMain().getTaskDesc());
        //负责人 id
        QueryWrapper<PsUser> userSy= Wrappers.query();
        userSy.eq("id",xi.getPsSolvetaskMain().getTaskManagerName());
        PsUser psUser=  psUserMapper.selectOne(userSy);
        //负责人code
        psSolvetaskMain.setTaskManagerCode(psUser.getUserCode());
        //负责人name
        psSolvetaskMain.setTaskManagerName(psUser.getUserName());
// 任务状态取决于 是否 添加了消缺员   有就是  已分配
        if (xi.getPsSolverDetail()!=null){
            //任务状态Id
            psSolvetaskMain.setTaskStatus(32);
            //任务状态name
            psSolvetaskMain.setTaskStatusName("已分配");
        }else {
            //任务状态Id
            psSolvetaskMain.setTaskStatus(31);
            //任务状态name
            psSolvetaskMain.setTaskStatusName("待分配");
        }
        psSolvetaskMain.setFinishTime(LocalDateTime.now());
        //备注       --前端赋值
        psSolvetaskMain.setCommon(xi.getPsSolvetaskMain().getCommon());
// 这四个属性添加的时候不用赋值
        //任务负责人审查意见 --为空不用赋值
        psSolvetaskMain.setManagerSuggestion(null);
        //完成情况描述     --完成情况描述
        psSolvetaskMain.setTaskFinishDesc(null);
        //工作终结报告     --为空赋值
        psSolvetaskMain.setTaskFinishReport(null);
        //工作简短延期记录  --为空不赋值'
        psSolvetaskMain.setTaskNotes(null);
        //下发人code
        psSolvetaskMain.setIssuedByCode(yongHu.getUserCode());
        //下发人name
        psSolvetaskMain.setIssuedByName(yongHu.getUserName());
        //任务下发时间
        psSolvetaskMain.setIssuedTime(LocalDateTime.now());
//为空不用赋值
        //下发人审查意见
        //是否取消  -- 1是 0 否
        psSolvetaskMain.setIsCancel(0);
        //创建时间  --当前时间
        psSolvetaskMain.setCreationTime(LocalDateTime.now());
        //创建者    --当前
        psSolvetaskMain.setCreatedBy(yongHu.getUserName());
        //最后修改时间 --初始插入的是当前时间
        psSolvetaskMain.setLastUpdateTime(LocalDateTime.now());
        int  row  =psSolvetaskMainMapper.insert(psSolvetaskMain);
        PsSolverDetail psSolverDetail=new PsSolverDetail();
        boolean fAlg=false;
        if(row>0){
            System.out.println(xi.getPsSolverDetail()+"abc");
            if(xi.getPsSolverDetail()!=null){
                //第二部  插入  消缺员子表
                for (ChuAnSuoKuAng chuAnSuoKuAng : xi.getPsSolverDetail()) {
                  //主表任务id
                  psSolverDetail.setTaskId(psSolvetaskMain.getId());
                  //消缺员名称 code
                  psSolverDetail.setSolverCode(chuAnSuoKuAng.getValue());
                  //消缺员名称 name
                  psSolverDetail.setSolverName(chuAnSuoKuAng.getTitle());
                  //创建时间
                  psSolverDetail.setCreationTime(LocalDateTime.now());
                  //创建人
                  psSolverDetail.setCreatedBy(yongHu.getUserName());
                  //最后修改时间
                  psSolverDetail.setLastUpdateTime(LocalDateTime.now());
                  //psSolvetaskDetailMapper.insert(psSolverDetail);
                    psSolverDetailMapper.insert(psSolverDetail);
                }
            }
            PsSolvetaskDetail psSolvetaskDetail1=new PsSolvetaskDetail();
            for (PsInspectiontaskDetail psInspectiontaskDetail : xi.getPsSolvetaskDetail()) {
                //主键自动生成
                //消缺主表id
                psSolvetaskDetail1.setTaskId(psSolvetaskMain.getId());
                //巡检任务子表id 前端有
                psSolvetaskDetail1.setInspectionTaskDelId(psInspectiontaskDetail.getId());
                //改变巡检任务子表的  is 否消缺状态
                //创建时间
                psSolvetaskDetail1.setCreationTime(LocalDateTime.now());
                //创建人
                psSolvetaskDetail1.setCreatedBy(yongHu.getUserName());
                //最后修改时间
                psSolvetaskDetail1.setLastUpdateTime(LocalDateTime.now());
                int row2=  psSolvetaskDetailMapper.insert(psSolvetaskDetail1);
                if(row2>0){
                    PsInspectiontaskDetail aa=new PsInspectiontaskDetail();
                    aa.setIsSolve(1);
                    QueryWrapper<PsInspectiontaskDetail> ziXunRenWu= Wrappers.query();
                    ziXunRenWu.eq("id",psInspectiontaskDetail.getId());
                    int gai= psInspectiontaskDetailMapper.update(aa,ziXunRenWu);
                    System.out.println(gai+"改成了吗");
                }
            }
            fAlg=true;
        }
        return fAlg;
    }

    @Override
    public List<PsSolverDetail> chaXiaoQuRen(Integer id) {
        QueryWrapper<PsSolverDetail> ziXunRenWu= Wrappers.query();
        ziXunRenWu.eq("taskId",id);
        List<PsSolverDetail> aa=  psSolverDetailMapper.selectList(ziXunRenWu);
        return aa;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean FenPeiXiAoQueRenWu(Integer id,List<ChuAnSuoKuAng> chuAnSuoKuAng,HttpSession httpSession) {
        int  row=0;
        for(ChuAnSuoKuAng aa:chuAnSuoKuAng){
            //第一步  增加   巡检员 消息   巡检员明细表
            PsSolverDetail  psSolverDetail=new PsSolverDetail();
            //主表id
            psSolverDetail.setTaskId(id);
            //巡检员 id
            psSolverDetail.setSolverCode(aa.getValue());
            //巡检员name
            psSolverDetail.setSolverName(aa.getTitle());
            //创建时间
            psSolverDetail.setCreationTime(LocalDateTime.now());
            PsUser  yongHu = (PsUser) httpSession.getAttribute("loginUser");
            //创建者
            psSolverDetail.setCreatedBy(yongHu.getUserName());
            //最后修改时间
            psSolverDetail.setLastUpdateTime(LocalDateTime.now());
            row+=  psSolverDetailMapper.insert(psSolverDetail);
        }
        if(row>0){
            PsSolvetaskMain psSolvetaskMain=new PsSolvetaskMain();
            psSolvetaskMain.setTaskStatus(36);
            psSolvetaskMain.setTaskStatusName("已分配");
            QueryWrapper<PsSolvetaskMain> ac= Wrappers.query();
            ac.eq("id",id);
            int row2= psSolvetaskMainMapper.update(psSolvetaskMain,ac);
            if(row2>0){
                return  true;
            }
        }
        //改变   主表任务状态
        return false;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean xiuGai(Integer id,ZhuRenWuXiaoQueMingXi xi, HttpSession httpSession) {
        PsUser  yongHu = (PsUser) httpSession.getAttribute("loginUser");
        //第一步先 插入主任务
        //任务编号    --前端赋值
        PsSolvetaskMain psSolvetaskMain=new PsSolvetaskMain();
        //消缺任务名称  --前端赋值
        psSolvetaskMain.setSolveTaskCode(xi.getPsSolvetaskMain().getSolveTaskCode());
        psSolvetaskMain.setSolveTaskName(xi.getPsSolvetaskMain().getSolveTaskName());
        //工作单据id   --前端赋值
        psSolvetaskMain.setWorkDocType(Integer.valueOf(xi.getPsSolvetaskMain().getWorkDocTypeName()));
        QueryWrapper<PsSystemconfig> sywr= Wrappers.query();
        sywr.eq("id",xi.getPsSolvetaskMain().getWorkDocTypeName());
        PsSystemconfig systemconfig =psSysTemConfigMapper.selectOne(sywr);
        //工作单据name --这个需要查
        psSolvetaskMain.setWorkDocTypeName(systemconfig.getConfigName());
        //任务描述     --前端赋值
        psSolvetaskMain.setTaskDesc(xi.getPsSolvetaskMain().getTaskDesc());
        //负责人 id
        QueryWrapper<PsUser> userSy= Wrappers.query();
        userSy.eq("id",xi.getPsSolvetaskMain().getTaskManagerName());
        PsUser psUser=  psUserMapper.selectOne(userSy);
        //负责人code
        psSolvetaskMain.setTaskManagerCode(psUser.getUserCode());
        //负责人name
        psSolvetaskMain.setTaskManagerName(psUser.getUserName());
// 任务状态取决于 是否 添加了消缺员   有就是  已分配
        if (xi.getPsSolverDetail()!=null){
            //任务状态Id
            psSolvetaskMain.setTaskStatus(32);
            //任务状态name
            psSolvetaskMain.setTaskStatusName("已分配");
        }else {
            //任务状态Id
            psSolvetaskMain.setTaskStatus(31);
            //任务状态name
            psSolvetaskMain.setTaskStatusName("待分配");
        }
        psSolvetaskMain.setFinishTime(LocalDateTime.now());
        //备注       --前端赋值
        psSolvetaskMain.setCommon(xi.getPsSolvetaskMain().getCommon());
// 这四个属性添加的时候不用赋值
        //任务负责人审查意见 --为空不用赋值
        psSolvetaskMain.setManagerSuggestion(null);
        //完成情况描述     --完成情况描述
        psSolvetaskMain.setTaskFinishDesc(null);
        //工作终结报告     --为空赋值
        psSolvetaskMain.setTaskFinishReport(null);
        //工作简短延期记录  --为空不赋值'
        psSolvetaskMain.setTaskNotes(null);
        //下发人code
        psSolvetaskMain.setIssuedByCode(yongHu.getUserCode());
        //下发人name
        psSolvetaskMain.setIssuedByName(yongHu.getUserName());
        //任务下发时间
        psSolvetaskMain.setIssuedTime(LocalDateTime.now());
//为空不用赋值
        //下发人审查意见
        //是否取消  -- 1是 0 否
        psSolvetaskMain.setIsCancel(0);
        //创建时间  --当前时间
        psSolvetaskMain.setCreationTime(LocalDateTime.now());
        //创建者    --当前
        psSolvetaskMain.setCreatedBy(yongHu.getUserName());
        //最后修改时间 --初始插入的是当前时间
        psSolvetaskMain.setLastUpdateTime(LocalDateTime.now());
        QueryWrapper<PsSolvetaskMain> ziXunRenWu= Wrappers.query();
        ziXunRenWu.eq("id",id);
        int  row  =psSolvetaskMainMapper.update(psSolvetaskMain,ziXunRenWu);
        PsSolverDetail psSolverDetail=new PsSolverDetail();
        boolean fAlg=false;
        if(row>0){
            System.out.println(xi.getPsSolverDetail()+"abc");
            if(xi.getPsSolverDetail()!=null){
                //第二部  插入  消缺员子表
                for (ChuAnSuoKuAng chuAnSuoKuAng : xi.getPsSolverDetail()) {
                    //主表任务id
                    psSolverDetail.setTaskId(id);
                    //消缺员名称 code
                    psSolverDetail.setSolverCode(chuAnSuoKuAng.getValue());
                    //消缺员名称 name
                    psSolverDetail.setSolverName(chuAnSuoKuAng.getTitle());
                    //创建时间
                    psSolverDetail.setCreationTime(LocalDateTime.now());
                    //创建人
                    psSolverDetail.setCreatedBy(yongHu.getUserName());
                    //最后修改时间
                    psSolverDetail.setLastUpdateTime(LocalDateTime.now());
                    psSolverDetailMapper.insert(psSolverDetail);
                }
            }
            PsSolvetaskDetail psSolvetaskDetail1=new PsSolvetaskDetail();
            for (PsInspectiontaskDetail psInspectiontaskDetail : xi.getPsSolvetaskDetail()){
                //先查询 这个对象这个有没有
                QueryWrapper<PsSolvetaskDetail> ziXunRenWu2= Wrappers.query();
                ziXunRenWu2.eq("id",id);
                int aa=  psSolvetaskDetailMapper.selectCount(ziXunRenWu2);
                if(aa>0){
                   continue;
                }
                //主键自动生成
                //消缺主表id
                psSolvetaskDetail1.setTaskId(id);
                //巡检任务子表id 前端有
                psSolvetaskDetail1.setInspectionTaskDelId(psInspectiontaskDetail.getId());
                //创建时间
                psSolvetaskDetail1.setCreationTime(LocalDateTime.now());
                //创建人
                psSolvetaskDetail1.setCreatedBy(yongHu.getUserName());
                //最后修改时间
                psSolvetaskDetail1.setLastUpdateTime(LocalDateTime.now());
                int row2=  psSolvetaskDetailMapper.insert(psSolvetaskDetail1);
                if(row2>0){
                    //改变巡检任务子表的  is 否消缺状态
                    PsInspectiontaskDetail psInspectiontaskDetail1=new PsInspectiontaskDetail();
                    psInspectiontaskDetail1.setIsSolve(1);
                    QueryWrapper<PsInspectiontaskDetail> ziXunRenWu222= Wrappers.query();
                    ziXunRenWu222.eq("id",psInspectiontaskDetail.getId());
                    int gai= psInspectiontaskDetailMapper.update(psInspectiontaskDetail1,ziXunRenWu222);
                }
            }
            fAlg=true;
        }
        return fAlg;
    }
}
